package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.collect.ListUtil;
import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.ComicCommentListController;
import com.qire.manhua.databinding.LayoutComicCommentListItemBinding;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.entity.ComicComment;
import com.qire.manhua.dal.service.ComicCommentListService;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;

public class ComicCommentListViewModel extends SmartRefreshViewModel implements IViewModel {

    private final ComicCommentListService commentListService = new ComicCommentListService(this);

    private final ArrayList<ComicCommentListItemViewModel> commentItemList = new ArrayList<>();
    private final SummerRecyclerBindAdapter<ComicCommentListItemViewModel> commentAdapter = new SummerRecyclerBindAdapter<>(commentItemList);

    private int pageNo = 1;
    private ComicBookEntity comicBookEntity;
    private int chapterId;


    @Override
    public int getVariableId() {
        return BR.comicCommentListViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     * @param controller 事件控制器
     */
    public void onAddition(ComicCommentListController controller) {
        commentAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        this.comicBookEntity = null;
        this.commentAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    public void loadComicInfo(ComicBookEntity comicBookEntity, int chapterId) {
        this.comicBookEntity = comicBookEntity;
        this.chapterId = chapterId;
    }

    public ComicBookEntity comicBookEntity() {
        return comicBookEntity;
    }

    public int comicBookId() {
        return ObjectUtil.nonNull(comicBookEntity) ? comicBookEntity.id : -1;
    }
    public int chapterId() {
        return chapterId;
    }

    public void nextPage(){
        this.pageNo++;
    }

    public int pageNo() {
        return this.pageNo;
    }

    public boolean isFirstPage() {
        return 1 == pageNo;
    }

    private void resetPage() {
        this.pageNo = 1;
    }

    public void refreshCommentData() {
        resetPage();
        commentListService.getCommentList();
    }

    public void moreCommentData() {
        commentListService.getCommentList();
    }

    /**
     * 清理漫画评论列表
     */
    public void clearComicComment() {
        commentAdapter.notifyItemRangeRemoved(0, commentItemList.size());
        commentItemList.clear();
    }

    /**
     * 加载评论信息
     * @param comments 漫画评论
     */
    public void loadComicComment(ComicComment[] comments) {
        if(comments == null) {
            return;
        }
        int startIndex = commentItemList.size(), count = comments.length;
        for(ComicComment comment : comments) {
            commentItemList.add(new ComicCommentListItemViewModel(comment));
        }
        commentAdapter.notifyItemRangeChanged(startIndex, count);
//        commentAdapter.notifyItemRangeInserted(startIndex, count);
        refreshCommentPanelUI();
    }


    public void refreshCommentPanelUI() {
        boolean isEmptyData = ListUtil.isEmpty(commentItemList);
        put("displayedChildPageNo", isEmptyData ? 1 : 0);
    }

    public MutableLiveData<Integer> getDisplayedChildPageNo() {
        return get("displayedChildPageNo");
    }

    public Adapter getCommentAdapter() {
        return commentAdapter;
    }

    public LayoutManager getCommentLayoutManager() {
        return new LinearLayoutManager(null);
    }


    @AdapterEntityBind(viewRid = R.layout.layout_comic_comment_list_item)
    public class ComicCommentListItemViewModel extends BaseObservable implements AutoVariable<LayoutComicCommentListItemBinding> {

        public final ComicComment comicComment;

        public ComicCommentListItemViewModel(ComicComment comicComment) {
            this.comicComment = comicComment;
        }

        @Override
        public void bindVariable(LayoutComicCommentListItemBinding viewBind) {
            viewBind.setComicCommentListItemViewModel(this);
        }

        public boolean isMyLike() {
            return comicComment.isMyLike();
        }

        public String userName() {
            return comicComment.userName;
        }

        public String content() {
            return comicComment.content.toString();
        }

        /**
         * 点赞
         */
        public void giveAlike() {
            comicComment.likeCountUp();
            commentListService.voteToGiveLikeComment(comicBookId(), comicComment.id);
            notifyPropertyChanged(BR.thumbsUpRes);
            notifyPropertyChanged(BR.likeCount);
        }

        /**
         * 举报
         * @param type 举报类型
         * @param content 举报内容
         */
        public void complaint(int type, String content) {
            commentListService.complaintComment(comicBookId(), comicComment.id, type, content);
        }

        @Bindable
        public int getPortraitFloatIconVisible(){
            return comicComment.isVip() ? View.VISIBLE : View.GONE;
        }
        @Bindable
        public String getPortraitPath() {
            return comicComment.userPortrait;
        }
        @Bindable
        public String getUserName(){
            return comicComment.userName;
        }
        @Bindable
        public String getUserNameColor(){
            return comicComment.isVip() ? "#ff5050" : "#777777";
        }

        @Bindable
        public CharSequence getContent() {
            return comicComment.content;
        }

        @Bindable
        public String getCreateTime(){
            return comicComment.createTime;
        }
        @Bindable
        public int getThumbsUpRes(){
            return comicComment.isMyLike() ? R.drawable.app_comic_detail_comment_icon_give_like_b : R.drawable.app_comic_detail_comment_icon_give_like_a;
        }
        @Bindable
        public String getLikeCount(){
            return StringUtil.toSymbol(comicComment.likeCount());
        }
        @Bindable
        public String getReplyCount(){
            return comicComment.replyCountToString();
        }

    }

}
